package cn.lf.android.lawfirm.inputsource.conntroller;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.request.ParseManager;
import cn.lf.android.lawfirm.http.request.RequestManager;
import cn.lf.android.lawfirm.utils.DebugLog;

/**
 * 拟委托方的conntroller
 * Created by Administrator on 2017/8/7.
 */

public class QuasiCommissionedPartyConntroller {

    private QuasiCommissionedPartyCallback quasiCommissionedPartyCallback;

    public QuasiCommissionedPartyConntroller(QuasiCommissionedPartyCallback quasiCommissionedPartyCallback) {
        this.quasiCommissionedPartyCallback = quasiCommissionedPartyCallback;
    }

    /**
     * 请求拟委托方页数据
     */
    public void onQuasicommissionedParty(String accessToken, String q) {
        new RequestManager().quasicommissionedParty(accessToken, q, new EBCallback() {

            @Override
            public void onEBResponse(String response) {
                DebugLog.e("你委托方搜索=="+response);
                if (null == response || "".equals(response)) {
                    quasiCommissionedPartyCallback.onFailed("请求失败");
                } else {
                    JSONObject myJsonObject;
                    Boolean isSuc;
                    try {
                        myJsonObject = new JSONObject(response);
                        isSuc = myJsonObject.getBoolean("isSuc");
                        if (isSuc) {
                            Map<String, Object> parseManagement = new ParseManager().parseQuasiCommissionedParty(response);
                            quasiCommissionedPartyCallback.onSuccessed(isSuc,
                                    (JSONArray) parseManagement.get("jsonArray"),
                                    (String) parseManagement.get("errorMsg"));
                        } else {
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void onEBError(String errorMsg) {
                quasiCommissionedPartyCallback.onFailed(errorMsg);
            }
        });

    }


    public interface QuasiCommissionedPartyCallback {
        public void onSuccessed(Boolean isSuc, JSONArray jsonArray, String errorMsg);

        public void onFailed(String errorMsg);
    }

}
